package com.iwhalecloud.bss.kite.cucc.web.controller;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalCallClientEnum;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalErrorConsts;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.zsmart.core.exception.BaseAppException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author sunshaoqiang
 */
@RestController
@RequestMapping("/exception")
@Api(tags = {"/exception - 异常"})
public class ExceptionController {

    @ApiOperation(value = "异常")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "errorCode", value = "错误编码", required = true),
        @ApiImplicitParam(name = "isCore", value = "是否核心异常", required = true),
    })
    @GetMapping
    public DataResult<String> exception(@RequestParam(required = true) String errorCode, @RequestParam(required = true) Integer isCore) throws BaseAppException {

        if (isCore == 1) {
            Arrays.stream(ErrorConsts.values()).forEach(code -> {
                if (code.getCode().equals(errorCode)) {
                    code.throwOut(code.getErrorCode(), code.getMsgCode());
                }
            });
        } else {
            Arrays.stream(LocalErrorConsts.values()).forEach(code -> {
                if (code.getCode().equals(errorCode)) {
                    code.throwOut(code.getErrorCode(), code.getMsgCode());
                }
            });
        }
        return ErrorConsts.SUCCESS.getResult(errorCode);
    }

    @ApiOperation(value = "异常列表")
    @GetMapping("list")
    public DataResult<List> list()  {
        List<Object> exceptionCode = new ArrayList<>();
        exceptionCode.addAll(Arrays.asList(ErrorConsts.getErrorConsts()));
        exceptionCode.addAll(Arrays.asList(LocalErrorConsts.getErrorConsts()));
        return ErrorConsts.SUCCESS.getResult(exceptionCode);
    }
    @ApiOperation(value = "异常列表")
    @GetMapping("SYSTEM_ERROR")
    public void systemError()  {
//        List list= new ArrayList<>();
//        list.get(2);
//        List list1= null;
//        list1.get(2);
        ErrorConsts.INF_INVOKE_EXCEPTION.throwOut("json_chinaUnicom_govEnterprise_marketingCenter_qryContractForOrder","接口返回报文缺少USER_INFO节点信息!");
        ErrorConsts.INF_INVOKE_ERROR.throwOut("json_chinaUnicom_govEnterprise_marketingCenter_qryContractForOrder", LocalCallClientEnum.INF_COMMON_URL.getServiceCode(), "查询合同接口失败");




//        1、无任何提示 以后禁止使用 注(在数据库表kite_error_msg配置才会提示，目前只有“业务通用异常”支持这种)
//        ErrorConsts.SYSTEM_ERROR.throwOut("系统异常系统异常");
//        2、可以抛出异常，并正常提示“正在处理...请稍候再试”；不建议使用
//        throw new BssException(KeyConsts.LOG_MODULE, ErrorConsts.SYSTEM_ERROR.getCode(), "正在处理...请稍候再试");








//        BssException.throwOut("", "", "A端标准地址编码不能为空");
//        BssException.throwOut(KeyConsts.LOG_MODULE, ErrorConsts.SYSTEM_ERROR.getCode(),"在表obj_event找不到记录，evenCode=");
    }
}
